package com.supor.aiot.common.app.api;

import com.android.baseconfig.base.BaseApiResult;
import com.android.baseconfig.base.BaseRetrofit;
import com.android.baseconfig.base.DataApiResult;
import com.android.baseconfig.common.config.HttpConstant;
import com.android.baseconfig.common.utils.SystemUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.entity.DeviceTree;
import com.supor.aiot.common.entity.ProductType;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.core.Observable;
import okhttp3.RequestBody;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: DeviceImpl <br>
 * 作者: lei <br>
 * 日期: 2020/11/7 <br>
 */
public class DeviceImpl extends BaseRetrofit<DeviceService> implements DeviceService {
    private static DeviceImpl instance;

    public static DeviceImpl getInstance() {
        if (instance == null) {
            instance = new DeviceImpl();
        }
        return instance;
    }

    /**
     * 获取设备列表树(/bossTAppliancesModel/json/page?limit=1000)
     *
     * @return Observable<BaseApiResult<DataApiResult<List<ProductType>>>>
     */
    @Override
    public Observable<BaseApiResult<DataApiResult<List<ProductType>>>> getAllDevice(int limit) {
        return api.getAllDevice(limit).compose(this.applySchedulers());
    }


    /**
     * 获取设备列表树(/bossTAppliancesModel/json/page?limit=1000)
     *
     * @return Observable<BaseApiResult<DataApiResult<List<ProductType>>>>
     */
    public Observable<BaseApiResult<DataApiResult<List<ProductType>>>> getAllProduct() {
        return getAllProduct(1000);
    }

    @Override
    public Observable<BaseApiResult<DataApiResult<List<ProductType>>>> getAllProduct(int limit) {
        return api.getAllProduct(limit).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<DeviceTree>> getBindDevice() {
        return api.getBindDevice().compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<String>> bind(Map<String, String> map) {
        return api.bind(map).compose(this.applySchedulers());
    }

    /**
     * 设备绑定
     *
     * @param
     *          mac         设备mac           设备侧读取到的mac
     *          thirdType   第三方类型         固定值Gizwits
     * @return Observable<BaseApiResult<String>>
     */
    public Observable<BaseApiResult<String>> bind(String mac) {
        Map<String, String> map = new HashMap<>();
        map.put("mac", mac);
        map.put("thirdType", "Gizwits");
        return bind(map);
    }

    @Override
    public Observable<BaseApiResult<String>> unbind(Map<String, String> map) {
        return api.unbind(map).compose(this.applySchedulers());
    }

    /**
     * 设备解绑
     *
     * @param
     *          mac	    设备mac           设备侧读取到的mac
     *          did	    设备唯一标识       设备侧读取到的did
     * @return Observable<BaseApiResult<String>>
     */
    public Observable<BaseApiResult<String>> unbind(String mac, String did) {
        Map<String, String> map = new HashMap<>();
        map.put("mac", mac);
        map.put("did", did);
        return unbind(map);
    }

    @Override
    public Observable<BaseApiResult<String>> queryStatus(String mac) {
        return api.queryStatus(mac).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<String>> apiQueryStatus(String mac) {
        return api.apiQueryStatus(mac).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<String>> pushBindResult(RequestBody requestBody) {
        return api.pushBindResult(requestBody).compose(this.applySchedulers1());
    }

    @Override
    public Observable<BaseApiResult<Boolean>> updateMac(String newMac, String oldMac, String thirdType) {
        return  api.updateMac(newMac, oldMac, thirdType).compose(this.applySchedulers());
    }

    /**
     * 上报绑定结果
     *
     * @param deviceId 设备唯一码
     * @param wifiType 配网类型: 1-nfc|2-app_nfc|3-app_ap|4-其他
     * @param wifiTime 配网时间: Ap从碰一碰配网开始计时, Nfc从发送密码到主厨机开始计时
     * @param isSuccess 配网是否成功: 1-成功|0-失败
     * @return
     */
    public Observable<BaseApiResult<String>> pushBindResult(String deviceId, String productMac,
                                                            String wifiType, int wifiTime,
                                                            String isSuccess) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("deviceId", deviceId);
            jsonObject.put("productMac", productMac);
            // 设备类型: 1-android|2-ios
            jsonObject.put("deviceType", "1");
            jsonObject.put("wifiType", wifiType);
            jsonObject.put("wifiTime", wifiTime);
            jsonObject.put("isSuccess", isSuccess);
            jsonObject.put("productType", "1");
            jsonObject.put("productModelNum", "SY-50MT01");
            jsonObject.put("appVersion", SystemUtils.getVerName(AppAgent.getApp()));
            RequestBody requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
            return pushBindResult(requestBody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
